//
// Created by vista on 2017/12/9.
//

#ifndef P_P5_H
#define P_P5_H

#include <vector>
#include <cstdlib>
#include <algorithm>
#include <string>
using namespace std;

class Solution {
public:
    string longestPalindrome(string s) {
        int maxlen = 0;

        if(s.length() == 0)
            return s;

        int ibegin = 0 , iend = 0;
        auto len = static_cast<int>(s.length());

        for (int i = 0; i < len; ++i) {

            int tmplen = 1;
            int j = 1;
            while ( i - j >= 0 && i +j < len ){
                if(s[i-j] == s[i+j]){
                    tmplen += 2;
                    j++;
                }else
                    break;
            }

            if(tmplen > maxlen){
                maxlen = tmplen;
                ibegin = i - j + 1;
                iend = i + j - 1;
            }
        }

        for (int i = 0; i < len; ++i) {

            int tmplen = 0;
            int j = 1;
            while ( i - j + 1 >= 0 && i + j < len ){
                if(s[i - j + 1] == s[i+j]){
                    tmplen += 2;
                    j++;
                }else
                    break;
            }

            if(tmplen > maxlen){
                maxlen = tmplen;
                ibegin = i - j + 2;
                iend = i + j - 1;
            }
        }
        return s.substr(ibegin,iend - ibegin + 1);
    }
};

#endif //P_P5_H
